package com.pwser.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pwser.dao.UserDao;
import com.pwser.dao.UserMapper;
import com.pwser.entity.User;
import com.pwser.test1.dao.UserMapperTest1;
import com.pwser.test2.dao.UserMapperTest2;

@Service
public class UserService {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UserMapperTest1 userMapperTest1;
	@Autowired
	private UserMapperTest2 userMapperTest2;
	@Autowired
	private UserService userService;
	
	public void findUser() {
		List<Map<String, Object>> userlist = jdbcTemplate.queryForList("SELECT USER_NAME FROM SHIRO_USER");
		System.out.println("JdbcTemplate查询结果------共"+userlist.size()+"条数据，第一条数据：" + userlist.get(0).get("USER_NAME"));
	}
	
	public void findUserJPA() {
		List<User> userlist = userDao.findAll();
		System.out.println("JPA查询结果-------共"+userlist.size()+"条数据，第一条数据：" + userlist.get(0).getUserName());
	}
	
	public void getAllUsersMyBatis() {
		List<User> userlist = userMapper.getAllUsers();
		System.out.println("MyBatis查询结果-------共"+userlist.size()+"条数据，第一条数据：" + userlist.get(0).getUserName());
	}
	
	public void findByNameMyBatis(String userName) {
		User user = userMapper.findByName(userName);
		System.out.println("MyBatis查询结果-------"+user.getUserName());
	}
	
	public void insertMyBatis(Integer userId, String userName) {
		userMapper.insert(userId, userName);
		System.out.println("-----MyBatis插入数据成功------");
	}
	
	public void getAllUsersTest1() {
		List<User> userlist = userMapperTest1.getAllUsers();
		System.out.println("数据源Test1查询结果-------共"+userlist.size()+"条数据，第一条数据：" + userlist.get(0).getUserName());
	}
	
	public void getAllUsersTest2() {
		List<User> userlist = userMapperTest2.getAllUsers();
		System.out.println("数据源Test2查询结果-------共"+userlist.size()+"条数据，第一条数据：" + userlist.get(0).getUserName());
	}
	
	@Transactional
	public void insertTest1(Integer userId, String userName) {
		userMapperTest1.insert(userId, userName);
		int i = 1 / 0;
		System.out.println("-----数据源Test1插入数据成功------");
	}
	
	//@Transactional("test2TransactionManager")
	@Transactional
	public void insertTest2(Integer userId, String userName) {
		userMapperTest2.insert(userId, userName);
		int i = 1 / 0;
		System.out.println("-----数据源Test2插入数据成功------");
	}
	
	@Transactional
	public void insertTest(Integer userId, String userName) {
		userMapperTest1.insert(userId, userName);
		userMapperTest2.insert(userId, userName);
		int i = 1 / 0;
	}

}